//Page 344

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
#include "Generators.h"

using namespace std ;

struct BRand
{
	bool operator() ( )
	{
		return rand() % 2 == 0 ;
	}
};

void print (char * pPrompt, vector<int> & x)
{
	cout<< pPrompt;
	cout.setf(ios::showpos | ios::right );
	cout.width(3);

	copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));
	cout<< endl ;
}

void printb (char * pPrompt, vector<bool> & x)
{	
	cout.setf(ios::boolalpha);
	cout<< pPrompt;
	copy(x.begin(), x.end(), ostream_iterator<bool>(cout, " "));
	cout<< endl ;
}

int main( ) 
{
	const int SZ = 10;
	const int MAX = 50;
	vector<int> x(SZ), y(SZ), r(SZ);

	URandGen urg(MAX);
	srand(time(0));
	generate_n(x.begin(), SZ, urg); //generate() is also a STL func.
	generate_n(y.begin(), SZ, urg);

	transform(y.begin(), y.end(), y.begin(), bind2nd(plus<int>(), 1) );
	print ("--------------vector x: ", x);
	print ("--------------vector y: ", y);
	
	transform(x.begin(), x.end(), y.begin(), r.begin(), plus<int>());
	print ("--After plus, vector r: ", r) ;

	transform(x.begin(), x.end(), r.begin(), negate<int>()) ;
	print ("--After nega, vector r: ", r) ;

	vector<bool> br(SZ);
	transform(x.begin(), x.end(), y.begin(), br.begin(), less_equal<int>());
	printb ("--After <=,   vector br: ", br) ;

	vector<bool> b1(SZ), b2(SZ);
	generate_n( b1.begin(), SZ, BRand() );
	generate_n( b2.begin(), SZ, BRand() );
	printb ("\n----vector b1: ", b1) ;
	printb ("----vector b2: ", b2) ;
	transform ( b1.begin(), b1.end(), b2.begin(), br.begin(), logical_and<int>() );
	printb ("-After &&, br: ", br );


	return 0 ;
}
